package com.hoshiicloud.user.service.impl.organization;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.user.entity.organization.PlatOrganizationCategory;
import com.hoshiicloud.user.entity.platform.Platform;
import com.hoshiicloud.user.feign.IdGeneratorService;
import com.hoshiicloud.user.mapper.organization.PlatOrganizationCategoryMapper;
import com.hoshiicloud.user.service.organization.PlatOrganizationCategoryService;
import com.hoshiicloud.user.service.platform.PlatformService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author:campchen
 * @Date:Created in 下午 04:11 2019/8/21
 * @PACKAGENAME com.hoshiicloud.user.service.impl.organization
 * @Description
 */
@Service
public class PlatOrganizationCategoryServiceImpl extends ServiceImpl<PlatOrganizationCategoryMapper, PlatOrganizationCategory> implements PlatOrganizationCategoryService {

    @Autowired
    private IdGeneratorService idGeneratorService;
    @Autowired
    private PlatformService platformService;
    @Autowired
    private PlatOrganizationCategoryMapper platOrganizationCategoryMapper;

    /**
     * @Author:campchen
     * @Description:
     * @Date:上午 10:45 2019/6/14
     * @Param:
     * @return:
     */
    @Override
    @Cacheable(cacheNames = "PlatOrganizationCategory", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Logable(authName = "后台用户操作-根据供应商经营类目信息进行分页查询")
    public IPage<PlatOrganizationCategory> getPage(IPage<PlatOrganizationCategory> page, QueryWrapper<PlatOrganizationCategory> queryWrapper, String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }

    /**
     * @param id
     * @return com.hoshiicloud.organization.entity.PlatOrganizationCategory
     * @description: 根据id查询供应商经营类目plat_organization_category
     */
    @Override
    @Cacheable(cacheNames = "PlatOrganizationCategory", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "供应商信息-供应商经营类目plat_organization_category -查询供应商经营类目plat_organization_category ")
    public PlatOrganizationCategory getById(Long id) {
        return baseMapper.selectById(id);
    }


    /**
     * @param entity
     * @return boolean
     * @description: 新增供应商经营类目plat_organization_category
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "PlatOrganizationCategory", allEntries = true)
    @Logable(authName = "供应商信息-供应商经营类目plat_organization_category -新增供应商经营类目plat_organization_category ")
    public boolean save(PlatOrganizationCategory entity) {
        String id = idGeneratorService.queryGeneratorId();
        if (StringUtils.isBlank(id)) {
            throw new ServiceException(CommonError.error("新增失败"));
        }

        Platform platform = platformService.getById(entity.getPlatformId());
        if (null == platform) {
            throw new ServiceException(CommonError.error("新增失败,租户有误"));
        }

        entity.setId(Long.parseLong(id));
        entity.setCreatedTime(LocalDateTime.now());
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增失败"));
        }
        return true;
    }

    /**
     * @param entity
     * @return boolean
     * @description: 修改供应商经营类目plat_organization_category
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "PlatOrganizationCategory", allEntries = true)
    @Logable(authName = "供应商信息-供应商经营类目plat_organization_category -修改供应商经营类目plat_organization_category ")
    public boolean updateById(PlatOrganizationCategory entity) {
        entity.setUpdatedTime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Logable(authName = "供应商信息-平台供应商经营类目plat_organization_category -批量新增平台供应商经营类目plat_organization_category ")
    @CacheEvict(cacheNames = "PlatOrganizationCategory", allEntries = true)
    public boolean insertCategory4PlatOrganization(List<PlatOrganizationCategory> platOrganizationCategories) {
        for (int i=0;i<platOrganizationCategories.size();i++){
            platOrganizationCategories.get(i).setCreatedTime(LocalDateTime.now());
            //1-未删除 0-已删除
            platOrganizationCategories.get(i).setDeleted(1);
            //1-可用 0-不可用
            platOrganizationCategories.get(i).setEnabled(1);
        }
        return platOrganizationCategoryMapper.insertCategory4PlatOrganization(platOrganizationCategories);
    }
}
